﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
//using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using NutriStat.Data;
using NutriStat.Statistics;
using NutriStat;

namespace NutriStat.Web
{
    public partial class _Default : System.Web.UI.Page
    {
        #region Data
        #region Protected Data
        #endregion Protected Data

        #region Private Data
        private NutriStat.NutriPatient patient;
        private NutriStat.NutriConfig config;
        private bool loadingRecord;          // Used to determine if we are navigating records or not
        private bool databaseLoaded;         // Used to determine if a database has been loaded yet
        private bool patientInfoChanged;     // Used to determine if basic patient data has been changed
        private bool resultsChanged;         // Used to determine if statistic results have changed
        private bool valueChanged;           // Used to determine if user-supplied value has changed
        #endregion Private Data

        #region Internal Data
        #endregion Internal Data
        #endregion Data

        #region Properties
        #region Public Properties
        /// <summary>
        /// Gets the patient object
        /// </summary>
        public NutriStat.NutriPatient Patient
        {
            get
            {
                return patient;
            }
        }

        /// <summary>
        /// Gets the form's configuration settings
        /// </summary>
        public NutriStat.NutriConfig Config
        {
            get
            {
                return config;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the user has changed a form control
        /// </summary>
        public bool InputsUpdated
        {
            get
            {
                return valueChanged;
            }
            set
            {
                valueChanged = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not NutriStat has changed statistic
        /// results
        /// </summary>
        public bool OutputsUpdated
        {
            get
            {
                return resultsChanged;
            }
            set
            {
                resultsChanged = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the user has changed basic patient
        /// information (inclues only non-visit information such as date
        /// of birth, gender, name, mother, father, address).
        /// </summary>
        public bool PatientInfoChanged
        {
            get
            {
                return patientInfoChanged;
            }
            set
            {
                patientInfoChanged = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not a database has been loaded.
        /// </summary>
        public bool DatabaseLoaded
        {
            get
            {
                return databaseLoaded;
            }
            set
            {
                databaseLoaded = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not a record is currently being loaded
        /// </summary>
        public bool LoadingRecord
        {
            get
            {
                return loadingRecord;
            }
            set
            {
                loadingRecord = value;
            }
        }
        #endregion Public Properties

        #region Protected Properties
        #endregion Protected Properties

        #region Private Properties
        #endregion Private Properties
        #endregion Properties

        #region Events
        protected void Page_Load(object sender, EventArgs e)
        {
            patient = new NutriPatient();
            LoadingRecord = false;
            InputsUpdated = false;
            OutputsUpdated = false;
            PatientInfoChanged = false;
            DatabaseLoaded = false;
            //dateTimePickerDOB.Checked = false;

            config = new NutriConfig();
            //NutriConfig.SetCurrentDirectory(Directory.GetCurrentDirectory());
            //ConfigureFormControls();
            //CheckResultsForNullValues();
        }

        protected void textBoxAgeMonths_TextChanged(object sender, EventArgs e)
        {
            InputsUpdated = true;
            Patient.Visit.Age = textBoxAgeMonths.Text;

            // Add code to disable 'Recumbent' check box and clear any check mark
            // like Epi 3 does if age > 36 months
            try
            {
                if (textBoxAgeMonths.Text.Length > 0)
                {
                    double ageMonths = 0.0;
                    ageMonths = Convert.ToDouble(textBoxAgeMonths.Text);
                    if (ageMonths > 36.001)
                    {
                        checkBoxRecumbent.Checked = false;
                        checkBoxRecumbent.Enabled = false;
                    }
                    else
                    {
                        checkBoxRecumbent.Enabled = true;
                    }
                }
                else
                {
                    checkBoxRecumbent.Enabled = true;
                }
            }
            catch
            {
                checkBoxRecumbent.Enabled = true;
            }

            RefreshData(0);
                
        }

        protected void textBoxHeight_TextChanged(object sender, EventArgs e)
        {
            InputsUpdated = true;
            if (string.IsNullOrEmpty(textBoxHeight.Text))
            {
                Patient.Visit.HeightCustomary = NutriStringConstants.Empty;
                Patient.Visit.HeightMetric = NutriStringConstants.Empty;
                Patient.Visit.BMI = NutriStringConstants.Empty;
            }
            RefreshData(0);
        }

        protected void textBoxWeight_TextChanged(object sender, EventArgs e)
        {
            InputsUpdated = true;
            if (string.IsNullOrEmpty(textBoxWeight.Text))
            {
                Patient.Visit.WeightCustomary = NutriStringConstants.Empty;
                Patient.Visit.WeightMetric = NutriStringConstants.Empty;
                Patient.Visit.BMI = NutriStringConstants.Empty;
            }
            RefreshData(0);
        }

        protected void textBoxHeadCircum_TextChanged(object sender, EventArgs e)
        {
            InputsUpdated = true;
            if (string.IsNullOrEmpty(textBoxHeadCircum.Text))
            {
                Patient.Visit.HeadCircumferenceCustomary = NutriStringConstants.Empty;
                Patient.Visit.HeadCircumferenceMetric = NutriStringConstants.Empty;
            }
            RefreshData(0);
        }

        protected void textBoxSSF_TextChanged(object sender, EventArgs e)
        {
            InputsUpdated = true;
            if (string.IsNullOrEmpty(textBoxSSF.Text))
            {
                Patient.Visit.SubscapularSkinFoldCustomary = NutriStringConstants.Empty;
                Patient.Visit.SubscapularSkinFoldMetric = NutriStringConstants.Empty;
            }
            RefreshData(0);
        }

        protected void textBoxTSF_TextChanged(object sender, EventArgs e)
        {
            InputsUpdated = true;
            if (string.IsNullOrEmpty(textBoxTSF.Text))
            {
                Patient.Visit.TricepsSkinFoldCustomary = NutriStringConstants.Empty;
                Patient.Visit.TricepsSkinFoldMetric = NutriStringConstants.Empty;
            }
            RefreshData(0);
        }

        protected void textBoxMUAC_TextChanged(object sender, EventArgs e)
        {

        }

        protected void textBoxDOM_TextChanged(object sender, EventArgs e)
        {

        }

        protected void dateTimePickerDOB_TextChanged(object sender, EventArgs e)
        {

        }

        protected void radioButtonMale_CheckedChanged(object sender, EventArgs e)
        {

        }

        protected void radioButtonFemale_CheckedChanged(object sender, EventArgs e)
        {

        }

        protected void textBoxFirstName_TextChanged(object sender, EventArgs e)
        {

        }

        protected void textBoxLastName_TextChanged(object sender, EventArgs e)
        {

        }

        protected void textBoxHAP_TextChanged(object sender, EventArgs e)
        {
            OutputsUpdated = true;
        }

        protected void textBoxHAZ_TextChanged(object sender, EventArgs e)
        {
            OutputsUpdated = true;
        }

        protected void textBoxWAP_TextChanged(object sender, EventArgs e)
        {
            OutputsUpdated = true;
        }

        protected void textBoxWAZ_TextChanged(object sender, EventArgs e)
        {
            OutputsUpdated = true;
        }

        protected void textBoxWHP_TextChanged(object sender, EventArgs e)
        {
            OutputsUpdated = true;
        }

        protected void textBoxWHZ_TextChanged(object sender, EventArgs e)
        {
            OutputsUpdated = true;
        }

        protected void textBoxBMIP_TextChanged(object sender, EventArgs e)
        {
            OutputsUpdated = true;
        }

        protected void textBoxBMIZ_TextChanged(object sender, EventArgs e)
        {
            OutputsUpdated = true;
        }

        protected void textBoxHeadP_TextChanged(object sender, EventArgs e)
        {
            OutputsUpdated = true;
        }

        protected void textBoxHeadZ_TextChanged(object sender, EventArgs e)
        {
            OutputsUpdated = true;
        }

        protected void textBoxSSFP_TextChanged(object sender, EventArgs e)
        {
            OutputsUpdated = true;
        }

        protected void textBoxSSFZ_TextChanged(object sender, EventArgs e)
        {
            OutputsUpdated = true;
        }

        protected void textBoxTSFP_TextChanged(object sender, EventArgs e)
        {
            OutputsUpdated = true;
        }

        protected void textBoxTSFZ_TextChanged(object sender, EventArgs e)
        {
            OutputsUpdated = true;
        }

        protected void textBoxMUACP_TextChanged(object sender, EventArgs e)
        {
            OutputsUpdated = true;
        }

        protected void textBoxMUACZ_TextChanged(object sender, EventArgs e)
        {
            OutputsUpdated = true;
        }

        protected void textBoxBMI_TextChanged(object sender, EventArgs e)
        {
            OutputsUpdated = true;
        }
        #endregion Events



        #region Methods
        /// <summary>
        /// Gets a format string based on the number of digits sent to the function.
        /// </summary>
        /// <param name="digits">Number of digits past the decimal point</param>
        /// <returns>string</returns>
        internal string GetPrecisionString(int digits)
        {
            #region Input Validation
            // Input validation
            //if (digits < 0 || digits > 5)
            //    throw new Exception("Parameter out of bounds in GetPrecisionString method. Value: " + digits.ToString());
            #endregion

            string format = "{0:0.00}";  // default to two digits of precision

            switch (digits)
            {
                case 0:
                    format = "{0:0}";
                    break;
                case 1:
                    format = "{0:0.0}";
                    break;
                case 2:
                    format = "{0:0.00}";
                    break;
                case 3:
                    format = "{0:0.000}";
                    break;
                case 4:
                    format = "{0:0.0000}";
                    break;
                case 5:
                    format = "{0:0.00000}";
                    break;
                default:
                    format = "{0:0.00}";
                    break;
            }

            return format;
        }   

        /// <summary>
        /// Refreshes all of the form's statistics and results.
        /// </summary>
        /// <param name="stats">The type of refresh to be conducted</param>
        internal void RefreshData(int stats)
        {
            // Depending on the number that gets passed in, we only refresh certain fields,
            // which saves us some compute power and improves efficiency. I did this because
            // doing a total refresh of all output boxes has a small but still noticeable
            // slowdown, and we want to avoid that if at all possible. 

            // 0 = Do everything
            // 1 = Age updated (will ential re-doing doing most measurements)
            // 2 = Sex updated (entails redoing all measurements)
            // 3 = Weight updated
            // 4 = Height updated
            // 5 = Head circumference updated  
            // 6 = MUAC updated
            // 7 = SSF updated
            // 8 = TSF updated

            #region Input Validation
            // Input validation
            if (stats < 0 || stats > 8)
                throw new Exception("Parameter out of bounds in RefreshData method. Value: " + stats.ToString());
            #endregion

            double BMI = -1.0;

            if (textBoxWeight.Text != NutriStringConstants.Empty && textBoxHeight.Text != NutriStringConstants.Empty && stats != 5 && stats != 6)
            {
                double height = -1.0;
                height = Convert.ToDouble(textBoxHeight.Text);

                double weight = -1.0;
                weight = Convert.ToDouble(textBoxWeight.Text);

                if (textBoxWeightOz.Text != NutriStringConstants.Empty && textBoxWeightOz.Text != NutriStringConstants.Empty)
                {
                    weight = weight + (Convert.ToDouble(textBoxWeightOz.Text) / 16);
                }

                if (config.UnitsWeight.Equals(MeasurementUnits.Pound.Abbreviation))
                    weight = weight * MeasurementUnits.Pound.ToKilograms();

                if (config.UnitsHeight.Equals(MeasurementUnits.Inch.Abbreviation))
                    height = height * MeasurementUnits.Inch.ToCentimeters();

                height = height / 100;
                BMI = (weight) / (height * height);
                string BMIstr = BMI.ToString();
                Patient.Visit.BMI = BMIstr;
                textBoxBMI.Text = FormatZScore(BMIstr);
            }
            else if (string.IsNullOrEmpty(textBoxWeight.Text) && string.IsNullOrEmpty(textBoxHeight.Text))
            {
                Patient.Visit.BMI = NutriStringConstants.Null;
                textBoxBMI.Text = NutriStringConstants.Undefined;
            }

            Patient.Visit.Age = textBoxAgeMonths.Text;
            try
            {
                Patient.Visit.VisitDate = Convert.ToDateTime(textBoxDOM.Text);
                Patient.BirthDate = Convert.ToDateTime(dateTimePickerDOB.Text);
            }
            catch
            {               
                return;
            }            

            if (radioButtonFemale.Checked)
                Patient.Gender = "F";
            else
                Patient.Gender = "M";

            if (checkBoxRecumbent.Checked)
                Patient.Visit.Recumbent = true;
            else
                Patient.Visit.Recumbent = false;

            if (checkBoxEdema.Checked)
                Patient.Visit.Edema = true;
            else
                Patient.Visit.Edema = false;

            // Update Height
            if (!string.IsNullOrEmpty(textBoxHeight.Text) && (stats == 4 || stats == 0))
            {
                if (config.UnitsHeight.Equals(MeasurementUnits.Centimeter.Abbreviation))
                {
                    Patient.Visit.HeightMetric = textBoxHeight.Text;
                    Patient.Visit.HeightCustomary = (Convert.ToDouble(textBoxHeight.Text) * MeasurementUnits.Centimeter.ToInches()).ToString();
                }
                else
                {
                    Patient.Visit.HeightMetric = (Convert.ToDouble(textBoxHeight.Text) * MeasurementUnits.Inch.ToCentimeters()).ToString();
                    Patient.Visit.HeightCustomary = textBoxHeight.Text;
                }
            }

            // Update Weight
            if (!string.IsNullOrEmpty(textBoxWeight.Text) && (stats == 3 || stats == 0))
            {
                if (config.UnitsWeight.Equals(MeasurementUnits.Kilogram.Abbreviation))
                {
                    Patient.Visit.WeightMetric = textBoxWeight.Text;
                    Patient.Visit.WeightCustomary = (Convert.ToDouble(textBoxWeight.Text) * MeasurementUnits.Kilogram.ToPounds()).ToString();
                }
                else
                {
                    Patient.Visit.WeightMetric = (Convert.ToDouble(textBoxWeight.Text) * MeasurementUnits.Pound.ToKilograms()).ToString();
                    Patient.Visit.WeightCustomary = textBoxWeight.Text;
                }
            }

            // Update Head circumference
            if (!string.IsNullOrEmpty(textBoxHeadCircum.Text) && (stats == 5 || stats == 0))
            {
                if (config.UnitsHead.Equals(MeasurementUnits.Centimeter.Abbreviation))
                {
                    Patient.Visit.HeadCircumferenceMetric = textBoxHeadCircum.Text;
                    Patient.Visit.HeadCircumferenceCustomary = (Convert.ToDouble(textBoxHeadCircum.Text) * MeasurementUnits.Centimeter.ToInches()).ToString();
                }
                else
                {
                    Patient.Visit.HeadCircumferenceMetric = (Convert.ToDouble(textBoxHeadCircum.Text) * MeasurementUnits.Inch.ToCentimeters()).ToString();
                    Patient.Visit.HeadCircumferenceCustomary = textBoxHeadCircum.Text;
                }
            }

            // MUAC updated
            if (!string.IsNullOrEmpty(textBoxMUAC.Text) && (stats == 6 || stats == 0))
            {
                if (config.UnitsArm.Equals(MeasurementUnits.Centimeter.Abbreviation))
                {
                    Patient.Visit.ArmCircumferenceMetric = textBoxMUAC.Text;
                    Patient.Visit.ArmCircumferenceCustomary = (Convert.ToDouble(textBoxMUAC.Text) * MeasurementUnits.Centimeter.ToInches()).ToString();
                }
                else
                {
                    Patient.Visit.ArmCircumferenceMetric = (Convert.ToDouble(textBoxMUAC.Text) * MeasurementUnits.Inch.ToCentimeters()).ToString();
                    Patient.Visit.ArmCircumferenceCustomary = textBoxMUAC.Text;
                }
            }

            // Subscapular skin fold
            if (!string.IsNullOrEmpty(textBoxSSF.Text) && (stats == 7 || stats == 0))
            {
                if (config.UnitsSkin.Equals(MeasurementUnits.Millimeter.Abbreviation))
                {
                    Patient.Visit.SubscapularSkinFoldMetric = textBoxSSF.Text;
                    Patient.Visit.SubscapularSkinFoldCustomary = (Convert.ToDouble(textBoxSSF.Text) * MeasurementUnits.Millimeter.ToInches()).ToString();
                }
                else
                {
                    Patient.Visit.SubscapularSkinFoldMetric = (Convert.ToDouble(textBoxSSF.Text) * MeasurementUnits.Inch.ToMillimeters()).ToString();
                    Patient.Visit.SubscapularSkinFoldCustomary = textBoxSSF.Text;
                }
            }

            // Triceps skin fold
            if (!string.IsNullOrEmpty(textBoxTSF.Text) && (stats == 8 || stats == 0))
            {
                if (config.UnitsSkin.Equals(MeasurementUnits.Millimeter.Abbreviation))
                {
                    Patient.Visit.TricepsSkinFoldMetric = textBoxTSF.Text;
                    Patient.Visit.TricepsSkinFoldCustomary = (Convert.ToDouble(textBoxTSF.Text) * MeasurementUnits.Millimeter.ToInches()).ToString();
                }
                else
                {
                    Patient.Visit.TricepsSkinFoldMetric = (Convert.ToDouble(textBoxTSF.Text) * MeasurementUnits.Inch.ToMillimeters()).ToString();
                    Patient.Visit.TricepsSkinFoldCustomary = textBoxTSF.Text;
                }
            }
            Patient.Visit.Gender = Patient.Gender;
            Patient.Visit.GenerateStatistics(stats);

            if (config.ReferenceType.Equals(GrowthReferenceTypes.CDC2000))
            {
                textBoxHeadZ.Text = FormatZScore(Patient.Visit.CDCHCircumZ);
                textBoxHAZ.Text = FormatZScore(Patient.Visit.CDCHAZ);
                textBoxWAZ.Text = FormatZScore(Patient.Visit.CDCWAZ);
                textBoxWHZ.Text = FormatZScore(Patient.Visit.CDCWHZ);
                textBoxBMIZ.Text = FormatZScore(Patient.Visit.CDCBMIZ);
                textBoxSSFZ.Text = NutriStringConstants.Undefined;
                textBoxTSFZ.Text = NutriStringConstants.Undefined;
                textBoxMUACZ.Text = NutriStringConstants.Undefined;
                textBoxHeadP.Text = FormatPercentile(Patient.Visit.CDCHCircumC);
                textBoxHAP.Text = FormatPercentile(Patient.Visit.CDCHAC);
                textBoxWAP.Text = FormatPercentile(Patient.Visit.CDCWAC);
                textBoxWHP.Text = FormatPercentile(Patient.Visit.CDCWHC);
                textBoxBMIP.Text = FormatPercentile(Patient.Visit.CDCBMIC);
                textBoxSSFP.Text = NutriStringConstants.Undefined;
                textBoxTSFP.Text = NutriStringConstants.Undefined;
                textBoxMUACP.Text = NutriStringConstants.Undefined;
                textBoxHAF.Text = CDC2000GrowthReference.CheckHeightAgeFlagged(Patient.Visit.CDCHAFlag);
                textBoxWAF.Text = CDC2000GrowthReference.CheckWeightAgeFlagged(Patient.Visit.CDCWAFlag);
                textBoxWHF.Text = CDC2000GrowthReference.CheckWeightHeightFlagged(Patient.Visit.CDCWHFlag);
                textBoxBMIF.Text = CDC2000GrowthReference.CheckBMIFlagged(Patient.Visit.CDCBMIFlag);
            }
            if (config.ReferenceType.Equals(GrowthReferenceTypes.WHO2007))
            {
                textBoxHeadZ.Text = NutriStringConstants.Undefined;
                textBoxHAZ.Text = FormatZScore(Patient.Visit.WHO2007HAZ);
                textBoxWAZ.Text = FormatZScore(Patient.Visit.WHO2007WAZ);
                textBoxWHZ.Text = NutriStringConstants.Undefined;
                textBoxBMIZ.Text = FormatZScore(Patient.Visit.WHO2007BMIZ);
                textBoxSSFZ.Text = NutriStringConstants.Undefined;
                textBoxTSFZ.Text = NutriStringConstants.Undefined;
                textBoxMUACZ.Text = NutriStringConstants.Undefined;
                textBoxHeadP.Text = NutriStringConstants.Undefined;
                textBoxHAP.Text = FormatPercentile(Patient.Visit.WHO2007HAC);
                textBoxWAP.Text = FormatPercentile(Patient.Visit.WHO2007WAC);
                textBoxWHP.Text = NutriStringConstants.Undefined;
                textBoxBMIP.Text = FormatPercentile(Patient.Visit.WHO2007BMIC);
                textBoxSSFP.Text = NutriStringConstants.Undefined;
                textBoxTSFP.Text = NutriStringConstants.Undefined;
                textBoxMUACP.Text = NutriStringConstants.Undefined;
                textBoxHAF.Text = WHO2007GrowthReference.CheckHeightAgeFlagged(Patient.Visit.WHO2007HAFlag);
                textBoxWAF.Text = WHO2007GrowthReference.CheckWeightAgeFlagged(Patient.Visit.WHO2007WAFlag);
                textBoxWHF.Text = NutriStringConstants.Empty;
                textBoxBMIF.Text = WHO2007GrowthReference.CheckBMIFlagged(Patient.Visit.WHO2007BMIFlag);
            }
            else if (config.ReferenceType.Equals(GrowthReferenceTypes.WHO2006))
            {
                textBoxHeadZ.Text = FormatZScore(Patient.Visit.WHO2006HCircumZ);
                textBoxHAZ.Text = FormatZScore(Patient.Visit.WHO2006HAZ);
                textBoxWAZ.Text = FormatZScore(Patient.Visit.WHO2006WAZ);
                textBoxWHZ.Text = FormatZScore(Patient.Visit.WHO2006WHZ);
                textBoxBMIZ.Text = FormatZScore(Patient.Visit.WHO2006BMIZ);
                textBoxSSFZ.Text = FormatZScore(Patient.Visit.WHO2006SSFZ);
                textBoxTSFZ.Text = FormatZScore(Patient.Visit.WHO2006TSFZ);
                textBoxMUACZ.Text = FormatZScore(Patient.Visit.WHO2006MUACZ);
                textBoxHeadP.Text = FormatPercentile(Patient.Visit.WHO2006HCircumC);
                textBoxHAP.Text = FormatPercentile(Patient.Visit.WHO2006HAC);
                textBoxWAP.Text = FormatPercentile(Patient.Visit.WHO2006WAC);
                textBoxWHP.Text = FormatPercentile(Patient.Visit.WHO2006WHC);
                textBoxBMIP.Text = FormatPercentile(Patient.Visit.WHO2006BMIC);
                textBoxSSFP.Text = FormatPercentile(Patient.Visit.WHO2006SSFC);
                textBoxTSFP.Text = FormatPercentile(Patient.Visit.WHO2006TSFC);
                textBoxMUACP.Text = FormatPercentile(Patient.Visit.WHO2006MUACC);
                textBoxHAF.Text = WHO2006GrowthReference.CheckHeightAgeFlagged(Patient.Visit.WHO2006HAFlag);
                textBoxWAF.Text = WHO2006GrowthReference.CheckWeightAgeFlagged(Patient.Visit.WHO2006WAFlag);
                textBoxWHF.Text = WHO2006GrowthReference.CheckWeightAgeFlagged(Patient.Visit.WHO2006WHFlag);
                textBoxBMIF.Text = WHO2006GrowthReference.CheckBMIFlagged(Patient.Visit.WHO2006BMIFlag);
                //textBoxHeadCircumF.Text = WHO2006GrowthReference.CheckHeadFlagged(Patient.Visit.WHO2006HeadFlag);
                //textBoxMUACF.Text = WHO2006GrowthReference.CheckMUACAgeFlagged(Patient.Visit.WHO2006MUACFlag);
                //textBoxSSFF.Text = WHO2006GrowthReference.CheckSSFAgeFlagged(Patient.Visit.WHO2006SSFFlag);
                //textBoxTSFF.Text = WHO2006GrowthReference.CheckTSFAgeFlagged(Patient.Visit.WHO2006TSFFlag);
            }
            else if (config.ReferenceType.Equals(GrowthReferenceTypes.WHO1978))
            {
                textBoxHeadZ.Text = NutriStringConstants.Undefined;
                textBoxHAZ.Text = FormatZScore(Patient.Visit.WHO1978HAZ);
                textBoxWAZ.Text = FormatZScore(Patient.Visit.WHO1978WAZ);
                textBoxWHZ.Text = FormatZScore(Patient.Visit.WHO1978WHZ);
                textBoxBMIZ.Text = NutriStringConstants.Undefined;
                textBoxSSFZ.Text = NutriStringConstants.Undefined;
                textBoxTSFZ.Text = NutriStringConstants.Undefined;
                textBoxMUACZ.Text = NutriStringConstants.Undefined;
                textBoxHeadP.Text = NutriStringConstants.Undefined;
                textBoxHAP.Text = FormatPercentile(Patient.Visit.WHO1978HAC);
                textBoxWAP.Text = FormatPercentile(Patient.Visit.WHO1978WAC);
                textBoxWHP.Text = FormatPercentile(Patient.Visit.WHO1978WHC);
                textBoxBMIP.Text = NutriStringConstants.Undefined;
                textBoxSSFP.Text = NutriStringConstants.Undefined;
                textBoxTSFP.Text = NutriStringConstants.Undefined;
                textBoxMUACP.Text = NutriStringConstants.Undefined;
            }

            //if (config.ResultsLegacyArm)
            //{
            //    textBoxMUACAge.Text = FormatZScore(Patient.Visit.MUACAgeZ);
            //    textBoxMUACHeight.Text = FormatZScore(Patient.Visit.MUACHeightZ);
            //}

            //UpdateFlags();

        } // end RefreshData()

        #region Private Methods
        /// <summary>
        /// Formats a z-score according to the program's settings XML file.
        /// </summary>
        /// <param name="input">The z-score to be formatted</param>        
        /// <returns>string</returns>
        private string FormatZScore(string input)
        {
            double num;

            try
            {
                num = Convert.ToDouble(input);
                num = Math.Round(num, config.PrecisionZ);
                input = String.Format(GetPrecisionString(config.PrecisionZ), num);
            }
            catch
            {
                return NutriStringConstants.Undefined;
            }
            return input;
        }

        /// <summary>
        /// Formats a percentile according to the program's settings XML file.
        /// </summary>
        /// <param name="input">The percentile to be formatted</param>        
        /// <returns>string</returns>
        private string FormatPercentile(string input)
        {
            try
            {
                double num;
                num = Convert.ToDouble(input);
                num = Math.Round(num, config.PrecisionC);
                if (num < 0 || num > 100)
                    return NutriStringConstants.Undefined;

                if (config.PrecisionC <= 1)
                {
                    if (num >= 99.9)
                        num = 99.9;
                }
                else if (config.PrecisionC == 2)
                {
                    if (num >= 99.99)
                        num = 99.99;
                }

                else if (config.PrecisionC == 3)
                {
                    if (num >= 99.999)
                        num = 99.999;
                }
                else
                {
                    if (num >= 99.9999)
                        num = 99.9999;
                }

                input = String.Format(GetPrecisionString(config.PrecisionC), num);
            }
            catch
            {
                return NutriStringConstants.Undefined;
            }
            return input;
        }
        #endregion Private Methods

        protected void buttonGraph_Click(object sender, EventArgs e)
        {
            RefreshData(0);
        }
        #endregion Methods



    }
}
